package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.baseservice.handler.exception.GuliException;
import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.ExcelSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.eduservice.vo.SubjectVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程分类列表 服务实现类
 * </p>
 *
 * @author dayouzi
 * @since 2022-09-18
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void importSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            //获取文件输入流
            InputStream inputStream = file.getInputStream();


            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubject.class, new SubjectExcelListener(subjectService)).sheet().doRead();

        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(ResultCodeEnum.FILE_UPLOAD_ERROR);
        }
    }

    @Override
    public List<SubjectVo> getNestedList() {
        //总分类数据（用于返回）
        ArrayList<SubjectVo> subjectVoArrayList = new ArrayList<>();

//        //查询所有数据
//        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
//        wrapper.orderByAsc("sort", "id");
//        List<EduSubject> allSubjectList = baseMapper.selectList(wrapper);

        //查询所有顶级父类数据
        QueryWrapper<EduSubject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id", "0").orderByAsc("sort", "id");
        List<EduSubject> parentSubjectList = baseMapper.selectList(wrapper1);

        //查询所有非顶级父类数据
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id", "0").orderByAsc("sort", "id");
        List<EduSubject> childrenSubjectList = baseMapper.selectList(wrapper2);

        //遍历
        for (EduSubject eduSubject : parentSubjectList) {
            //遍历所有顶级父类
            SubjectVo subjectVo = new SubjectVo();

            //判断顶级父类是否有子集
            boolean flag = hasChildrenSubject(eduSubject, childrenSubjectList);
            if (flag) {
                //获取其所有子集
                List<SubjectVo> childrenList = getChildrenList(eduSubject, childrenSubjectList);

                //转换vo
                BeanUtils.copyProperties(eduSubject, subjectVo);
                subjectVo.setChildren(childrenList);

                //存入总结果集
                subjectVoArrayList.add(subjectVo);
            }

        }
        return subjectVoArrayList;
    }

    /**
     * 判断是否有子集
     *
     * @param eduSubject
     * @param list
     * @return
     */
    private boolean hasChildrenSubject(EduSubject eduSubject, List<EduSubject> list) {
        //遍历整个集合
        for (EduSubject subject : list) {
            if (subject.getParentId().equals(eduSubject.getId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取某条数据的子集
     *
     * @param eduSubject
     * @param list 非顶级父类数据
     * @return
     */
    private List<SubjectVo> getChildrenList(EduSubject eduSubject, List<EduSubject> list) {
        //用于存放传入数据的子集
        List<SubjectVo> childrenList = new ArrayList<>();

        //遍历整个集合
        for (EduSubject subject : list) {
            //用于将遍历的数据转换为vo
            SubjectVo subjectVo = new SubjectVo();

            //判断是传入数据的子集的数据
            if (subject.getParentId().equals(eduSubject.getId())) {
                //遍历数据转换vo
                BeanUtils.copyProperties(subject, subjectVo);
                //将遍历子集存入传入数据的子集
                childrenList.add(subjectVo);

                //判断遍历数据是否有子集
                boolean flag = hasChildrenSubject(subject, list);
                if (flag) {
                    //递归调用，获取遍历数据的子集
                    List<SubjectVo> childrenList1 = getChildrenList(subject, list);
                    //将遍历数据的子集，存入子集中
                    subjectVo.setChildren(childrenList1);
                }
            }
        }

        //返回传入数据的子集
        return childrenList;
    }


}
